import json,csv,yaml,os,logging,time
from dotenv import load_dotenv

# 加载csv文件数据
def load_csv(file_path):
    with open(file_path,'r',newline='',encoding='utf-8') as f:
        r = csv.DictReader(f)
        return [i for i in r]
        # return list(csv.DictReader(f))

# 加载json文件数据
def load_json(file_path):
    with open(file_path,'r',encoding='utf-8') as f:
        return json.load(f)

# 加载yaml文件数据
def load_yaml(file_path):
    with open(file_path,'r',encoding='utf-8') as f:
        return yaml.safe_load(f)

def load_env_data():
    """加载.env文件中的环境变量"""
    load_dotenv()
    return {
        "WMS_USERNAME": os.getenv("WMS_USERNAME"),
        "WMS_PASSWORD": os.getenv("WMS_PASSWORD")
    }

# log日志
t = time.strftime("%Y%m%d%H%M%S",time.localtime())
def log_cat():
    logging.basicConfig(level=logging.INFO,
                              filename=f'./log/{t}.log',
                              # filename='./log/aa.log',
                              filemode='w+',
                              format='%(message)s %(levelname)s %(lineno)s %(filename)s %(asctime)s'
                              )
    hander = logging.StreamHandler()
    hander.setLevel(logging.INFO)

    # 使用相同的格式
    format = logging.Formatter('%(message)s %(levelname)s %(lineno)s %(filename)s %(asctime)s')
    hander.setFormatter(format)

    # 获取根日志记录器并添加控制台处理器
    logger = logging.getLogger()
    logger.addHandler(hander)



